package com.sharemarking.application.common.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.BaseSpecification;
import com.sharemarking.application.common.dto.DepartmentDto;
import com.sharemarking.application.common.entity.Company;
import com.sharemarking.application.common.entity.Department;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.repository.CompanyRepo;
import com.sharemarking.application.common.repository.DepartmentRepo;
import com.sharemarking.application.common.repository.EmployeeRepo;


@Controller
@RequestMapping("phone")
public class DepartmentCon {
	
	@Autowired
	private DepartmentRepo dao;
	@Autowired
	private EmployeeRepo dao1;
	@Autowired
	private CompanyRepo cr;
	
	//列表
	@RequestMapping(value="getAllDepart")
	@ResponseBody
	public List<Department> getAllDepart(Department e){
		List<Department> departs=dao.findAll(new BaseSpecification<Department>(e));
		
		return departs;
		
	}
	//增
	@RequestMapping(value="addDepart")
	@ResponseBody
	public String addDepart(String name,String manager,String describer,Integer companyid){
		if(name!=null){
			Department d=new Department();
			Company c=cr.findOne(companyid);
			d.setName(name);
			d.setManager(manager);
			d.setDescriber(describer);
			d.setCompany(c);
			dao.save(d);
			return "ok";
		}
		return "no";
	}
	//改
	@RequestMapping(value="updateDepart")
	@ResponseBody
	public String updateDepart(Integer id,String name,String manager,String describer){
		Department department=dao.findOne(id);
		department.setName(name);
		department.setManager(manager);
		department.setDescriber(describer);
		dao.save(department);
		return "ok";
	}
	//删
	@RequestMapping(value="delDepart")
	@ResponseBody
	public String delDepart(Integer id){
		//DepartmentDto department=dao.findOne(id);
		dao.delete(id);
		return "ok";
	}
	//查
	@RequestMapping(value="findDepart")
	@ResponseBody
	public Department findDepart(Integer id){
		Department dto=dao.findOne(id);
		System.out.println(dto);
		return dto;
	}
}
